#include "stdafx.h"
#include "csString.h"

//------------------------------------------------------------------------
void 
csMemCpy( c8* dst, const c8* src, s32 n )
{
	memcpy(dst,src,n);
}

//------------------------------------------------------------------------
void 
csMemCpy( c16* dst, const c16* src, s32 n )
{
	wmemcpy(dst,src,n);
}

//------------------------------------------------------------------------
s32 
csStrLen( const c8* s )
{
	csASSERT(s);
	return strlen(s);
}

//------------------------------------------------------------------------
s32 
csStrLen( const c16* s )
{
	csASSERT(s);
	return wcslen(s);
}

//------------------------------------------------------------------------
void
csMemSet( c8* dst, c8 ch, s32 len )
{
	memset(dst,ch,len);
}

//------------------------------------------------------------------------
void 
csMemSet( c16* dst, c16 ch, s32 len )
{
	wmemset(dst,ch,len);
}

//------------------------------------------------------------------------
s32 
csStrCmp(const c8* lhs,const c8* rhs )
{
	csASSERT(lhs);
	csASSERT(rhs);
	return strcmp(lhs,rhs);
}

//------------------------------------------------------------------------
s32 
csStrCmp(const c16* lhs,const c16* rhs )
{
	csASSERT(lhs);
	csASSERT(rhs);
	return wcscmp(lhs,rhs);
}

//------------------------------------------------------------------------
s32 
csStrCaseCmp(const c8* lhs, const c8* rhs )
{
	csASSERT(lhs);
	csASSERT(rhs);
	s32 i=0;
	while(lhs[i]||rhs[i])
	{
		if(csToLower(lhs[i])<csToLower(rhs[i]))
		{
			return -1;
		}
		else if(csToLower(lhs[i])>csToLower(rhs[i]))
		{
			return 1;
		}
		++i;
	}
	return 0;
}

//------------------------------------------------------------------------
s32 
csStrCaseCmp(const c16* lhs, const c16* rhs )
{
	csASSERT(lhs);
	csASSERT(rhs);
	s32 i=0;
	while(lhs[i]||rhs[i])
	{
		if(csToLower(lhs[i])<csToLower(rhs[i]))
		{
			return -1;
		}
		else if(csToLower(lhs[i])>csToLower(rhs[i]))
		{
			return 1;
		}
		++i;
	}
	return 0;
}

//------------------------------------------------------------------------
c8 
csToLower( c8 c )
{
	return (c>='A'&&c<='Z')
		? (c8)(c + ('a' - 'A'))
		: c;
}

//------------------------------------------------------------------------
c16 
csToLower( c16 c )
{
	return (c>=L'A'&&c<=L'Z')
		? (c16)(c + (L'a' - L'A'))
		: c;
}

//------------------------------------------------------------------------
c8 
csToUpper( c8 c )
{
	return (c>='a'&&c<='z')
		? (c8)(c - ('a' - 'A'))
		: c;
}

//------------------------------------------------------------------------
c16 
csToUpper( c16 c )
{
	return (c>=L'a'&&c<=L'z')
		? (c16)(c - (L'a' - L'A'))
		: c;
}

//------------------------------------------------------------------------
s32 
csVsnprintf(c8* buf, s32 count, const c8* format, va_list args )
{
	return _vsprintf_p(buf,count,format,args);
}

//------------------------------------------------------------------------
s32 
csVsnprintf(c16* buf, s32 count, const c16* format, va_list args )
{
	return _vswprintf_p(buf,count,format,args);
}

//------------------------------------------------------------------------
s32
csMemCmp(const c8* sz1, const c8* sz2, s32 len)
{
	return memcmp(sz1,sz2,len);
}

//------------------------------------------------------------------------
s32
csMemCmp(const c16* sz1, const c16* sz2, s32 len)
{
	return wmemcmp(sz1,sz2,len);
}

//------------------------------------------------------------------------
c8*
csMemChr(const c8* sz1, c8 ch, s32 count)
{
	return (c8*)memchr(sz1,ch, count);
}

//------------------------------------------------------------------------
c16*
csMemChr(const c16* sz1, c16 ch, s32 count)
{
	return (c16*)wmemchr(sz1,ch,count);
}

//------------------------------------------------------------------------
csCString 
csStringHelper::Unicode32ToAnsi( const csDisplayString& ustr )
{
	return csCString();
}

//------------------------------------------------------------------------
csCString
csStringHelper::Utf16ToAnsi(const csWString& wstr)
{
#ifdef _WIN32
	s32 len = WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,wstr.c_str(),-1,NULL,0,NULL,NULL);
	csCharBuffer buf(len);
	WideCharToMultiByte(CP_ACP,WC_COMPOSITECHECK,wstr.c_str(),-1,buf.data(),len,NULL,NULL);
#else
#endif
	return csCString(buf.data());
}

//------------------------------------------------------------------------
csCString
csStringHelper::Utf8ToAnsi(const csCString& utf)
{
	return csCString();
}

//------------------------------------------------------------------------
csCString
csStringHelper::Unicode32ToUtf8(const csDisplayString& ustr)
{
	return csCString();
}

//------------------------------------------------------------------------
csCString
csStringHelper::Utf16ToUtf8(const csWString& wstr)
{
#ifdef _WIN32
	s32 len = WideCharToMultiByte(CP_UTF8,WC_COMPOSITECHECK,wstr.c_str(),-1,NULL,0,NULL,NULL);
	csCharBuffer buf(len);
	WideCharToMultiByte(CP_UTF8,WC_COMPOSITECHECK,wstr.c_str(),-1,buf.data(),len,NULL,NULL);
#else
#endif
	return csCString(buf.data());
}

//------------------------------------------------------------------------
csCString
csStringHelper::AnsiToUtf8(const csCString& ansi)
{
	return csCString();
}

//------------------------------------------------------------------------
csWString
csStringHelper::Unicode32ToUtf16(const csDisplayString& ustr)
{
	return csWString();
}

//------------------------------------------------------------------------
csWString
csStringHelper::Utf8ToUtf16(const csCString& utf8)
{
#ifdef _WIN32
	s32 len = MultiByteToWideChar(CP_UTF8,MB_PRECOMPOSED,utf8.c_str(),-1,NULL,0) - 1;
	csWCharBuffer buf(len);
	MultiByteToWideChar(CP_UTF8,MB_PRECOMPOSED,utf8.c_str(),-1,buf.data(),len);
#else
#endif
	return csWString(buf.data());
}

//------------------------------------------------------------------------
csWString
csStringHelper::AnsiToUtf16(const csCString &ansi)
{
#ifdef _WIN32
	s32 len = MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,ansi.c_str(),-1,NULL,0) - 1;
	csWCharBuffer buf(len);
	MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,ansi.c_str(),-1,buf.data(),len);
#else
#endif
	return csWString(buf.data());
}

//------------------------------------------------------------------------
csDisplayString
csStringHelper::Utf16ToUnicode32(const csWString &wstr)
{
	return csDisplayString();
}

//------------------------------------------------------------------------
csDisplayString
csStringHelper::Utf8ToUnicode32(const csCString &utf8)
{
	return csDisplayString();
}

//------------------------------------------------------------------------
csDisplayString
csStringHelper::AnsiToUnicode32(const csCString &ansi)
{
	return csDisplayString();
}